﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interop.UIAutomationCore;
using UIAControls;
using LEETCustomProviders;

namespace LEETCustomPatterns
{
    class RotatePatternSchema : CustomPatternSchemaBase
    {
        private static RotatePatternSchema instance = null;
        public static RotatePatternSchema Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new RotatePatternSchema();
                    instance.Register();
                }
                return instance;
            }
        }


        public readonly UiaPropertyInfoHelper RotationProperty =
            new UiaPropertyInfoHelper(
                new Guid("931A481A-0950-4CBC-8567-ECDC99F0A94A"),
                "Rotation",
                UIAutomationType.UIAutomationType_Int);

        public readonly UiaMethodInfoHelper SetRotationMethod =
            new UiaMethodInfoHelper(
                "SetRotation",
                true /* doSetFocus */,
                new UiaParameterDescription[] {
                    new UiaParameterDescription("value", UIAutomationType.UIAutomationType_Int)
                });

        public readonly UiaMethodInfoHelper RotateMethod =
            new UiaMethodInfoHelper(
                "Rotate",
                true /* doSetFocus */,
                new UiaParameterDescription[] {
                    new UiaParameterDescription("value", UIAutomationType.UIAutomationType_Int)
                });

        public override UiaPropertyInfoHelper[] Properties
        {
            get { return new UiaPropertyInfoHelper[] { RotationProperty }; }
        }

        public override UiaMethodInfoHelper[] Methods
        {
            get { return new UiaMethodInfoHelper[] { SetRotationMethod, RotateMethod }; }
        }

        public override UiaEventInfoHelper[] Events
        {
            get { return new UiaEventInfoHelper[] { }; }
        }

        public override Guid PatternGuid
        {
            get { return new Guid("AA268D13-EE28-49A2-9FA8-AFBFA9C3A652"); }
        }

        public override Guid PatternClientGuid
        {
            get { return typeof(IRotationPattern).GUID; }
        }

        public override Guid PatternProviderGuid
        {
            get { return typeof(IRotationProvider).GUID; }
        }

        public override string PatternName
        {
            get { return "RotationPattern"; }
        }

        public override IUIAutomationPatternHandler Handler
        {
            get { return new RotationProviderHandler(); }
        }
    }

    public class RotationProviderHandler : Interop.UIAutomationCore.IUIAutomationPatternHandler
    {
        #region IUIAutomationPatternHandler Members

        public void CreateClientWrapper(Interop.UIAutomationCore.IUIAutomationPatternInstance pPatternInstance, out object pClientWrapper)
        {
            pClientWrapper = new RotationPattern(pPatternInstance);
        }

        public void Dispatch(object pTarget, uint index, IntPtr pParams, uint cParams)
        {
            // Parse the provider and parameter list
            IRotationProvider provider = (IRotationProvider)pTarget;
            UiaParameterListHelper paramList = new UiaParameterListHelper(pParams, cParams);

            // Dispatch the method/property calls
            if (index == RotatePatternSchema.Instance.RotationProperty.Index)
            {
                paramList[0] = provider.AngleInDegrees;
            }
            else if (index == RotatePatternSchema.Instance.SetRotationMethod.Index)
            {
                provider.SetRotation((int)paramList[0]);
            }
            else if (index == RotatePatternSchema.Instance.RotateMethod.Index)
            {
                provider.Rotate((int)paramList[0]);
            }
            else
            {
                throw new InvalidOperationException();
            }
        }

        #endregion
    }

    
}
